com.google.appengine.api.channel.ChannelServiceFactory Java Examples

The following examples show how to use com.google.appengine.api.channel.ChannelServiceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChannelHandler.java    From raspberrypi-appengine-portal with Apache License 2.0 6 votes vote down vote up
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	// In the handler for _ah/channel/connected/
	ChannelService channelService = ChannelServiceFactory.getChannelService();
	ChannelPresence presence = channelService.parsePresence(request);
	
	String clientId = presence.clientId();
	boolean connected = presence.isConnected();
	
	log.info("Client = " + clientId + ", connected = " + connected);
	
	if(!connected) {
		// remove them from the datastore
	    Client.remove(clientId);
	}
}
 
Example #2
Source File: SensorDataEndpoint.java    From raspberrypi-appengine-portal with Apache License 2.0 5 votes vote down vote up
@ApiMethod(name = "data.create", httpMethod = "post")
public SensorData create(SensorData data, User user) {
	
    // check the user is authenticated and authorised
	if(user == null) {
		log.warning("User is not authenticated");
		throw new RuntimeException("Authentication required!");
		
	} else if(!Constants.EMAIL_ADDRESS.equals(user.getEmail())) {
		
		log.warning("User is not authorised, email: " + user.getEmail());
		throw new RuntimeException("Not authorised!");
	}
	
	data.save();
	
	try {
	    // notify the client channels
	    ChannelService channelService = ChannelServiceFactory.getChannelService();

	    List<Client> clients = Client.findAll();
	    String json = GSON.toJson(data);

	    for(Client client: clients) {
	        channelService.sendMessage(new ChannelMessage(client.getId(), json));
	    }
	    
	} catch(Exception e) {
	    log.log(Level.SEVERE, "Failed to notify connected clients", e);
	}
		
	return data;
}
 
Example #3
Source File: ChatServlet.java    From gae-chat with MIT License 5 votes vote down vote up
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
	ChannelService channelService = ChannelServiceFactory.getChannelService();
	
	//The token must be based on some unique identifier for the client - I am using the current time
	//for demo purposes...
	String clientId = String.valueOf(System.currentTimeMillis());
    String token = channelService.createChannel(clientId);
    
    //Subscribe this client
    MultichannelChatManager.addSub(clientId);
    
    //Reply with the token
	res.setContentType("text/plain");
	res.getWriter().print(token);
}
 
Example #4
Source File: MultichannelChatManager.java    From gae-chat with MIT License 5 votes vote down vote up
public static void sendMessage(String body, String source) {
	Iterator<String> it = subs.iterator();
	while (it.hasNext()) {
		String sub = it.next();
		String messageBody = source + ": " + body;

		// We assume an at symbol is an XMPP client...
		if (sub.indexOf("@") >= 0) {
			JID jid = new JID(sub);
			Message msg = new MessageBuilder().withRecipientJids(jid).withBody(messageBody).build();
			XMPPService xmpp = XMPPServiceFactory.getXMPPService();
			xmpp.sendMessage(msg);
		}
		
		// If it starts with a "+" it's an SMS number...
		else if (sub.startsWith("+")) {
			TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");

			Map<String, String> params = new HashMap<String, String>();
			params.put("Body", messageBody);
			params.put("To", sub);
			params.put("From", "+16122948105");

			SmsFactory messageFactory = client.getAccount().getSmsFactory();

			try {
				Sms message = messageFactory.create(params);
				System.out.println(message.getSid());
			} catch (TwilioRestException e) {
				e.printStackTrace();
			}
		}

		// Otherwise, it's a browser-based client
		else {
			ChannelService channelService = ChannelServiceFactory.getChannelService();
			channelService.sendMessage(new ChannelMessage(sub,messageBody));
		}
	}
}
 
Example #5
Source File: StartServlet.java    From raspberrypi-appengine-portal with Apache License 2.0 4 votes vote down vote up
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // get the session 
    HttpSession session = request.getSession();
    String clientId = session.getId();

    log.info("Sending data to client " + clientId);

    Client client = Client.findById(clientId);

    String token = null;

    if(client == null) {

        ChannelService channelService = ChannelServiceFactory.getChannelService();
        log.info("Creating channel for clientId " + clientId);
        // open the channel
        token = channelService.createChannel(clientId);

        client = new Client(clientId, token);
        client.save();
    }
    
 // use the session id and the channel id for this client
    Map<String, Object> data = new HashMap<>();
    token = client.getToken();
    data.put("token", token);

    //createTestData();
    int minutes = 1;
    String timeRange = request.getParameter("timeRange");

    try {
        minutes = Integer.parseInt(timeRange);
        
    } catch(NumberFormatException e) {
        log.warning("Failed to parse number " + timeRange);
    }

    Calendar calendar = new GregorianCalendar();
    calendar.add(Calendar.MINUTE, -minutes);
    Date date = calendar.getTime();
    
    List<SensorData> sensorDataList = SensorData.findByDateTime(date);

    data.put("data", sensorDataList);

    response.setContentType(CONTENT_TYPE_JSON);
    response.getWriter().print(GSON.toJson(data));

}
 
Example #6
Source File: PresenceServlet.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    ChannelPresence presence = channelService.parsePresence(req);
    log.info(presence.clientId() + ":" + presence.isConnected());
}